package com.boyue.service.impl;

import com.boyue.dao.DatabaseUtil;
import com.boyue.dao.SysUserDao;
import com.boyue.dao.impl.SysUserDaoImpl;
import com.boyue.entity.SysUser;
import com.boyue.service.SysUserService;
import com.boyue.utils.PageInfo;

import java.sql.SQLException;

/**
 * @auth wzy
 * @date 2023/9/6 15:31
 */
public class SysUserServiceImpl implements SysUserService {

    private SysUserDao dao = new SysUserDaoImpl();

    @Override
    public SysUser login(String username, String password) throws RuntimeException {

        SysUser sysUser = null;
        try {
            sysUser = dao.findByUsername(username);

            if (sysUser == null){
                throw new RuntimeException("用户名不存在");
            }

            // 判断密码是否相等
            if (!password.equals(sysUser.getPassword())){
                throw new RuntimeException("密码错误");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            DatabaseUtil.closeConnection();
        }

        return sysUser;
    }

    @Override
    public PageInfo<SysUser> listPage(PageInfo pageInfo) {
        try {
            return dao.listPage(pageInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.closeConnection();
        }
        return null;
    }
}
